import { getLoginPerson, loginUser, GetCcmsUserInfo, getApp, getMenu, enterprise } from "@/api/common/common";
import store from "@/store/store"
import router from "@/router/index"
import { Message } from 'element-ui'

export function loginResponse(token) {
    let user = {}

    store.commit("changeGenerate", false);
    user.token = token;
    axios.defaults.headers.common["token"] = user.token;
    axios.defaults.headers.common["authorization"] = user.token;
    Promise.all([GetCcmsUserInfo(), loginUser(), getApp()]).then(resA => {
        // 个人信息
        let res1 = resA[0];
        // 用户所在组织
        let res2 = resA[1];
        // 一级目录
        let res3 = resA[2];
        if (res1.data && res2.data && res2.data.orgList && res3.data) {
            // 存储个人信息
            localStorage.userInfo = JSON.stringify(res1.data)

            user.loginId = res1.data.userAccountId;
            user.empId = res1.data.empId;
            user.loginName = res1.data.name;
            let orgId;
            res2.data.orgList.forEach(v => {
                if (v.orgCode === "NFZL") {
                    orgId = v.orgId;
                }
            });
            let appid;
            res3.data.forEach(v => {
                if (v.appCode === "ccms-smartleasing") {
                    appid = v.appId;
                }
            })
            user.appid = appid;
            //若没有南方权限 和 智慧租赁应用 则报错
            if (orgId && appid) {
                return Promise.all([enterprise(orgId), getMenu({ appId: appid })]);
            } else {
                return Promise.reject('缺失权限');
            }
        } else {
            return Promise.reject('登录失败');
        }
    }).then(resB => {
        // 用户所在管理处
        let res1 = resB[0];
        // 用户菜单
        let res2 = resB[1];

        // 拿到管理处权限
        user.jurisdiction = res1.data.property3;

        // 拿到菜单权限
        user.MenuNode = [];
        let pAll = [];
        res2.data.forEach(v => {
            let node = {
                nodeId: v.nodeId,
                nodeUrl: v.nodeUrl,
                nodeName: v.nodeName,
                nodeIcon: v.nodeIcon,
                leafNode: v.leafNode,
                childrenNode: []
            };
            pAll.push(getMenu({ appId: user.appid, upNodeId: v.nodeId }));
            user.MenuNode.push(node);
        })
        return Promise.all(pAll);
    }).then(resC => {
        for (let i = 0; i < resC.length; i++) {
            let pMenus = resC[i].data;
            let pChildrenNode = [];
            pMenus.forEach(v => {
                let node = {
                    nodeId: v.nodeId,
                    nodeUrl: v.nodeUrl,
                    nodeName: v.nodeName,
                    nodeIcon: v.nodeIcon,
                    leafNode: v.leafNode
                };
                pChildrenNode.push(node);
            })
            user.MenuNode[i].childrenNode = pChildrenNode;
        }
        store.commit('changeUser', user);
        store.commit('changeAdmin', null);
        return getLoginPerson(user);
    }).then(resD => {
        Message({ message: '登陆成功!', type: 'success', });
        if (router.currentRoute && router.currentRoute.query && router.currentRoute.query.ReturnUrl) {
            router.push(router.currentRoute.query.ReturnUrl);
        } else {
            router.push("/")
        }
    }).catch((resE) => {
        Message({ message: resE || 'Error', type: 'error', });
        if (router.currentRoute && router.currentRoute.path === "/redirect") {
            router.push("/login")
        }
    });
}